package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.core.hfb.HfbConst;
import com.atguigu.srb.core.pojo.entity.LendReturn;
import com.atguigu.srb.core.service.LendReturnService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 还款记录表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2023-03-25
 */
@RestController
@RequestMapping("/api/core/lendReturn")
public class ApiLendReturnController {
    /*
        权限控制
            将数据库CRUD的操作全都写到一个后端管理服务中

            用户前端需要操作数据时 提交给 后端 用户端服务接收处理，然后远程访问 上一个服务的接口
     */
    @Resource
    LendReturnService lendReturnService;
    //http://192.168.1.171/api/core/lendReturn/notifyUrl
    @ApiOperation("还款回调")
    @PostMapping("/notifyUrl")
    public String notifyUrl(@RequestParam Map<String,Object> map){
        return lendReturnService.notifyUrl(map);
    }
    //构建自动提交的表单字符串
    @ApiOperation("还款")
    @PutMapping("/borrowReturn/{id}")
    public ResponseVo borrowReturn(@PathVariable("id")Long id,
                                   @RequestHeader(value = "token",required = false)String token){
        String form  = lendReturnService.borrowReturn(id,token);
        return ResponseVo.ok().data("form",form);
    }


    @ApiOperation("查询标的的还款计划")
    @GetMapping("listLendReturns/{lendId}")
    public ResponseVo listLendReturns(@PathVariable("lendId")Long lendId){

        List<LendReturn> lendReturns = lendReturnService.list(Wrappers.lambdaQuery(LendReturn.class)
                .eq(LendReturn::getLendId, lendId)
                .orderByAsc(LendReturn::getCurrentPeriod));
        return ResponseVo.ok().data("items",lendReturns);
    }
}

